package mhserver;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class DatagramServer {
	private final static int	PACKETSIZE	= 100;

	public static void main(String args[]) {
		// Check the arguments
		try {
			// Convert the argument to ensure that is it valid
			int port = 45000;//Integer.parseInt( args[0] ) ;

			// Construct the socket
			DatagramSocket socket = new DatagramSocket(port);

			System.out.println("The server is ready...");

			for (;;) {
				// Create a packet
				DatagramPacket packet = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE);

				// Receive a packet (blocking)
				socket.receive(packet);

				// Print the packet
				System.out.println(packet.getAddress() + " " + packet.getPort() + ": " + new String(packet.getData()));

				// Return the packet to the sender
				socket.send(packet);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}
